/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include "song.h"
#include "debug.h"



/**
 * @brief Create new song with filename
 */
Song *song_new(const char *filename) {
    Song *S = (Song *) calloc(1, sizeof(Song));
    S->file = strdup(filename);
    INIT_LIST_HEAD(&S->all_song_node);
    INIT_LIST_HEAD(&S->album_node);
    INIT_LIST_HEAD(&S->play_list_node);
    INIT_LIST_HEAD(&S->shuffle_list_node);
    debug("Creating Song: %s\n", filename);
    return S;
}


/**
 * @brief delete this song and free the memory
 */
void song_free(Song *S) {
    free(S->file);
    list_del(&S->all_song_node);
    list_del(&S->album_node);
    list_del(&S->play_list_node);
    list_del(&S->shuffle_list_node);
    free(S);
}


/**
 * @brief return the dirname of the song file, it's a clone in which need to be free
 */
char *song_dirname(Song *S) {
    char * ptr;
    if (!S || !S->file)
        return NULL;
    ptr = strdup(S->file);
    return dirname(ptr);
}
